from utils import json, os

def Video(obj):
	return {
		"title": obj.get("title"),
		"id": obj.get("id"),
		"description": obj.get("description")
	}

async def get_video(url, path=None):
	"Return a list with a dictionary for each video extracted."
	filename = "video.json"
	if path is not None:
		filename = f"{path}/{filename}"

	if not os.path_exists(filename):
		command = f'youtube-dl --print-json --skip-download "{url}" > "{filename}"'
		await os.system(command)

	video = await json.load(filename)

	return Video(video)

async def get_playlist(url, path=None):
	filename = "playlist.json"
	if path is not None:
		filename = f"{path}/{filename}"

	if not os.path_exists(filename):
		command = f'youtube-dl --print-json --skip-download "{url}" > {filename}'
		await os.system(command)

	content = await os.file_read(filename)

	videos = []
	for line in content.split("\n"):
		if line.strip() == "":
			continue
		video = json.loads(line)
		videos.append(Video(video))

	return {
		"hash": hash,
		"videos": videos
	}
